#include "StatePlaying.hpp"
#include "Application.hpp"

namespace MA {

StatePlaying::StatePlaying() {
}

void StatePlaying::bindApp(Application* app) {
    m_app = app;
    m_app->setBGColor(0xff, 0xff, 0xff, 0xff);

    m_ttt.loadResource(m_app->getRenderPtr());
}

void StatePlaying::updateLogic() {
    switch (m_ttt.update()) {
    case '=':
        SDL_Log("X = O!\n");
        m_app->setRunning(false);
        break;
    case 'x':
        SDL_Log("Player x win!\n");
        m_app->setRunning(false);
        break;
    case 'o':
        SDL_Log("Player o win!\n");
        m_app->setRunning(false);
        break;
    }
}

void StatePlaying::updateRender() {
    m_app->clear();
    m_ttt.draw(m_app->getRenderPtr());
    m_app->flush();
}

void StatePlaying::onKeyDown(SDL_Keycode keycode) {
    switch (keycode) {
    case SDLK_j:
        break;
    case SDLK_k:
        break;
    }
}

void StatePlaying::onMouseDown(uint8_t buttonIndex, int32_t mousePosX, int32_t mousePosY) {
    switch (buttonIndex) {
    case SDL_BUTTON_LEFT:
        m_ttt.setMap(uint8_t((mousePosX-16)/146), uint8_t((mousePosY-16)/146));
//        m_ttt.switchPlayer();
        break;
    case SDL_BUTTON_RIGHT:
        SDL_Log("Right button\n");
        SDL_Log("Mouse position (%d, %d)\n", mousePosX, mousePosY);
      break;
    case SDL_BUTTON_MIDDLE:
        SDL_Log("Middle button\n");
        SDL_Log("Mouse position (%d, %d)\n", mousePosX, mousePosY);
        break;
    default:
        SDL_Log("Mouse position (%d, %d)\n", mousePosX, mousePosY);
        break;
    }
}


}
